﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace User_subsystem
{
    public partial class FormUserselfmodify : Form
    {
        public FormUserselfmodify()
        {
            InitializeComponent();
        }
        private bool IsStringobsessnumber(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] > '0' && str[i] < '9')
                {
                    return false;
                }
            }
            return true;

        }
        private bool IsStringobsessword(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if ((str[i] > 'a' && str[i] < 'z') || (str[i] > 'A' && str[i] < 'Z'))
                {
                    return false;
                }
            }
            return true;
        }
        private bool IsnoStringobsessnumber(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] < '0' || str[i] > '9')
                {
                    return false;
                }
            }
            return true;

        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormUserselfmodify_Load(object sender, EventArgs e)
        {
            string userid = FormLogin.id;
            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"select Address,ContactNum,UserName from UserInfor where UserID='{userid}'";

            SqlDataReader reader = dao.read(sql);
            reader.Read();
            textaddress.Text =  reader[0].ToString();
            textphone.Text = reader[1].ToString();
            textname.Text = reader[2].ToString();
            reader.Close();
            dao.DaoClose();
        }

        private void buttonmodify_Click(object sender, EventArgs e)
        {
            //判断文本框是否有空
            if (textname.Text == ""  || textaddress.Text == "" || textphone.Text == "")
            {
                MessageBox.Show("有空项！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //名字是否含有数字
           else if (!(IsStringobsessnumber(textname.Text)))
            {
                //名字含有数字
                MessageBox.Show("名字含有数字", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            } 
            //电话号码含有非数字
            else if(!(IsnoStringobsessnumber(textphone.Text)))
            {
                //电话号码含有非数字
                MessageBox.Show("电话号码含有非数字", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //电话号码不是11位
            else if(textphone.Text.Length != 11)
            {
                MessageBox.Show("电话号码不是11位", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                DaoAdmin dao = new DaoAdmin();
                dao.connect();
                string sql = $"update [UserInfor]  set UserName ='{textname.Text.Trim()}' " +
                    $",[Address] ='{textaddress.Text.Trim()}',[ContactNum] ='{textphone.Text.Trim()}' where [UserID]='{FormLogin.id}'";

                if (dao.Execute(sql) > 0)
                {
                    MessageBox.Show("修改成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    //修改失败   
                    MessageBox.Show("修改失败！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                dao.DaoClose();
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
